package org.sheep;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.sheep.sprite.LaneManager;
import org.sheep.sprite.unit.Sheep;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class SpawnManagerFactory {
  private String file = null;
  private LaneManager lanes;
  
  public SpawnManagerFactory(LaneManager lm){
    lanes = lm;
  }
  
  public SpawnManagerFactory(LaneManager lm, String file) {
    this(lm);
    this.file = file;
  }

  public SpawnManager build() throws XmlPullParserException, IOException {
    SpawnManager sm = new SpawnManager(lanes);
    
    if (file != null){
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XmlPullParser xpp = factory.newPullParser();

      xpp.setInput(new InputStreamReader(new FileInputStream(file)));
      int eventType = xpp.getEventType();

      while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_DOCUMENT) {
          Log.i("WorldMapFactory", "Start document");
        } else if (eventType == XmlPullParser.START_TAG) {
          if(xpp.getName().equals("Level")) {
            tagLevel();
          }
          Log.i("WorldMapFactory", "Start tag " + xpp.getName());
        } else if (eventType == XmlPullParser.END_TAG) {
          Log.i("WorldMapFactory", "End tag " + xpp.getName());
        } else if (eventType == XmlPullParser.TEXT) {
          Log.i("WorldMapFactory", "Text " + xpp.getText());
        }
        eventType = xpp.next();
      }
    } else {
      ArrayList<Spawn> ar = new ArrayList<Spawn>(1);
      ar.add(new Spawn(new Sheep(), 0, 6, 1000, 1000, 500));
      
      sm.addWave(ar);
    }
    
    return sm;
  }
  
  private void tagLevel() {
    
  }
}
